package com.example.controller;

import com.example.dao.StudentDao;
import com.example.entity.Classes;
import com.example.entity.Student;
import com.example.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("stu")

public class StudentController {
    @Resource
    StudentService studentService;
    @Resource
    StudentDao studentDao;
    @RequestMapping("select")
    public String select(Model model,Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list= studentService.select();
        PageInfo<Student> pageInfo = new PageInfo<Student>(list);
        model.addAttribute("list",list);
        model.addAttribute("pageInfo",pageInfo);
        return "forward:/stuList.jsp";
    }
    @RequestMapping("selectC")
    public String selectC(Model model){
        List<Classes> list= studentService.selectC();
        model.addAttribute("list",list);
        return "forward:/stuAdd.jsp";
    }
    @RequestMapping("add")
    @ResponseBody
    public String add(Student student){
        System.out.println(student.toString());
       int result=studentService.addStu(student);
       if(result!=0) {
           return "<script>parent.location.href='/stu/select?pageNum=1&pageSize=5'</script>";
       }
       return "redirect:/error.jsp";
    }
    @RequestMapping("del")
    public String del(String stuno){
        int result=studentService.del(stuno);
        if(result!=0) {
            return "redirect:/stu/select";
        }
        return "redirect:/error.jsp";
    }
    @RequestMapping("updStu")
    public String upd(String stuno,Model model){
        List<Student> list=studentService.findByStuno(stuno);
        model.addAttribute("list",list);
        return "forward:/stuUpd.jsp";
    }
    @RequestMapping("updStu1")
    public String upd1(Student student){
        int result=studentService.updStu1(student);
        if(result!=0){
            return "redirect:/stu/select";
        }
        return "redirect:/error.jsp";

    }
    @RequestMapping("selectLike")
    public String selectLike(String stuno){
        System.out.println(stuno);
        System.out.println(studentDao.selectLike(stuno));
        return "redirect:/error.jsp";

    }
}
